package com.fr.data.core.db.handler;

import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreConstants;
import com.fr.data.core.db.BinaryObject;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.report.core.UploadedImage;
import com.fr.util.Utils;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class BlobTypeHandler
  implements SQLTypeHandler
{
  public void setValue(PreparedStatement paramPreparedStatement, int paramInt1, Object paramObject, int paramInt2, Dialect paramDialect)
    throws SQLException
  {
    byte[] arrayOfByte1 = new byte[0];
    if (paramObject instanceof byte[])
    {
      arrayOfByte1 = (byte[])paramObject;
    }
    else if ((paramObject instanceof InputStream) || (paramObject instanceof Blob))
    {
      if (paramObject instanceof Blob)
        paramObject = ((Blob)paramObject).getBinaryStream();
      try
      {
        byte[] arrayOfByte2 = new byte[32768];
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        while ((i = ((InputStream)paramObject).read(arrayOfByte2)) != -1)
        {
          int i;
          localByteArrayOutputStream.write(arrayOfByte2, 0, i);
        }
        arrayOfByte1 = localByteArrayOutputStream.toByteArray();
      }
      catch (IOException localIOException1)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException1.getMessage(), localIOException1);
      }
    }
    else if (paramObject instanceof UploadedImage)
    {
      arrayOfByte1 = ((UploadedImage)paramObject).getBytes();
    }
    else if (paramObject instanceof BufferedImage)
    {
      localObject = new ByteArrayOutputStream();
      try
      {
        if (ImageIO.write((BufferedImage)paramObject, "png", (OutputStream)localObject))
        {
          arrayOfByte1 = ((ByteArrayOutputStream)localObject).toByteArray();
          ((ByteArrayOutputStream)localObject).close();
        }
      }
      catch (IOException localIOException2)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException2.getMessage(), localIOException2);
      }
    }
    else if (paramObject instanceof BinaryObject)
    {
      arrayOfByte1 = ((BinaryObject)paramObject).getBytes();
    }
    else
    {
      arrayOfByte1 = "" + paramObject.getBytes();
    }
    Object localObject = new ByteArrayInputStream(arrayOfByte1);
    paramPreparedStatement.setBinaryStream(paramInt1, (InputStream)localObject, arrayOfByte1.length);
  }

  public Object getValue(ResultSet paramResultSet, int paramInt1, int paramInt2, Dialect paramDialect)
    throws SQLException
  {
    if (paramInt2 == 2004)
      return paramResultSet.getBlob(paramInt1);
    try
    {
      return new BinaryObject(Utils.inputStream2Bytes(paramResultSet.getBinaryStream(paramInt1)));
    }
    catch (Throwable localThrowable)
    {
    }
    return BaseCoreConstants.getDefaultImage();
  }
}